package com.zk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zk.entity.AllProducts;
import com.zk.entity.Evaluation;
import com.zk.mapper.AllProductsMapper;
import com.zk.mapper.EvaluationMapper;
import com.zk.service.IAllEvaluationService;
import com.zk.vo.EvaluationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//public class AllEvaluationServiceImpl implements IAllEvaluationService {

/**
 * @author  fmy
 */

/**
 * you had better to add a @Transactional annotation
 */
@Service
@Transactional
public class AllEvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements IAllEvaluationService {
    @Autowired
    private EvaluationMapper evaluationMapper;
    @Autowired
    private AllProductsMapper allProductsMapper;


    @Override
    public EvaluationVo getEvaluationListPageByProductId(Integer current,Integer size,Integer productId) {
        //you must give a null initiation,or run it second will cause an errer
        AllProducts firstProduct = null;

        List<AllProducts> allProductsList = null;

        IPage<Evaluation> evaluationPage = null;

        List<Evaluation> evaluationListByProductId = null;
        /**
         * use queryWrapper to implement where condition in query
         */
        QueryWrapper<Evaluation> evaluationByProductIdQueryWrapper = new QueryWrapper<>();
        evaluationByProductIdQueryWrapper.eq("evaluation_product_id",productId).eq("is_del",0);

        try {
            //  page类第三个构造函数是orderByField
            evaluationPage = this.baseMapper.getEvaluationWithUserList(new Page<Evaluation>(current,size),evaluationByProductIdQueryWrapper,productId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(evaluationPage == null){
            evaluationPage = new Page<Evaluation>();
        }
        /**
         * resultSet will store in evaluationPage or maybe not,but we can call it and get the result
         */
        evaluationListByProductId = evaluationPage.getRecords();
        if(evaluationListByProductId == null){
            evaluationListByProductId.add(new Evaluation());
        }


        EvaluationVo evaluationVo = new EvaluationVo();
        /**
         * we only need one product info by one product id,so it is unnecessary to make a join
         */
        QueryWrapper<AllProducts> allProductsByProductIdQueryWrapper = new QueryWrapper<>();
        allProductsByProductIdQueryWrapper.eq("product_id",productId).eq("is_del",0);

        /**
         * remember that:you must use try catch block to get some exceptions
         */
        try {
             allProductsList = allProductsMapper.selectList(allProductsByProductIdQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(firstProduct == null) {
            firstProduct = new AllProducts();
            // to do , give a warning if there is not a product
        }
        else{
            firstProduct = allProductsList.get(0);
        }
        //evaluationMapper.selectPage(evaluationPage,evaluationByProductIdQueryWrapper);
        evaluationVo.setCurrent(current);
        evaluationVo.setSize(size);
        evaluationVo.setTotal(evaluationPage.getTotal());
        evaluationVo.setEvaluationList(evaluationListByProductId);
        evaluationVo.setAllProducts(firstProduct);
        return evaluationVo;
    }

    @Override
    public EvaluationVo getEvaluationListByPageUseSQL(Integer current, Integer size, Integer productId) {

        return null;
    }
}
